package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.Clazz;
import com.itheima.entity.ClazzQueryParam;
import com.itheima.entity.PageBean;
import com.itheima.mapper.ClazzMapper;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 分页查询
     * @param classParam
     * @return
     */
    @Override
    public PageBean page(ClazzQueryParam clazzParam){
        //查询并返回数据
        PageHelper.startPage(clazzParam.getPage(), clazzParam.getPageSize());
        //查询数据
        List<Clazz> list = clazzMapper.selectByPage(clazzParam);
        Page p = (Page) list;

        return new PageBean(p.getTotal(), p.getResult());

    }

    /**
     * 添加班级
     * @param clazz
     */
    @Override
    public void save(Clazz clazz) {
        //补全数据
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());

        //保存数据
        clazzMapper.save(clazz);
    }

    /**
     * 查询所有班级
     * @return
     */
    @Override
    public List<Clazz> list() {
        List<Clazz> clazzList = clazzMapper.list();
        //查询数据
        return clazzList;
    }

    /**
     * 根据id查询班级
     * @param id
     * @return
     */
    @Override
    public Clazz getById(Integer id) {
        //查询数据
        return clazzMapper.getById(id);
    }

    /**
     * 更新班级
     * @param clazz
     */
    @Override
    public void update(Clazz clazz) {
       clazz.setUpdateTime(LocalDateTime.now());

       clazzMapper.update(clazz);
    }

    /**
     * 删除班级
     * @param id
     */
    @Override
    public void delete(Integer id) {
        clazzMapper.delete(id);
    }
}
